//
//  LKAddressPickerView.m
//  LKIntelligentVideoProject
//
//  Created by she1323 on 2018/11/7.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKAddressPickerView.h"
#import "LKPickerCityModel.h"
#import "LKPickerAreaModel.h"
#define kPickerViewHeight 200
#define kTitleHeight 50

@interface LKAddressPickerView()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic, strong) UIPickerView *pickerV;
@property (nonatomic, strong) NSMutableArray <LKPickerCityModel *>*allDataArr;
@property (nonatomic, strong) NSMutableArray *provinceArr;
@property (nonatomic, strong) NSMutableArray *cityArr;
@property (nonatomic, strong) NSMutableArray <LKPickerAreaModel *>*areaArr;
@property (nonatomic, strong)NSString *selectRegionId;

@property (nonatomic, strong) NSString *currentSelectProvince;

@property (nonatomic, strong) NSString *currentSelectCity;

@property (nonatomic, strong) LKPickerAreaModel *currentSelectArea;



// 布局控件
@property (nonatomic, strong) UIButton *bgV;
@end


@implementation LKAddressPickerView

-(instancetype)initWithPicker{
    if (self = [super init]) {
        self.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        [self getDataList];
        [self setupView];
       
    }
    return self;
}
-(void)show{
    [[[UIApplication sharedApplication] keyWindow] addSubview:self];
}

#pragma mark -- UIPickerView Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.provinceArr.count;
//    }else if (component == 1) {
//        return self.cityArr.count;
//    }else {
//        return self.areaArr.count;
//    }
    
    
    if (component == 0) {
        return self.cityArr.count;
    }else {
        return self.areaArr.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//    if (component == 0) {
//        return self.provinceArr[row];
//    }else if (component == 1) {
//        return self.cityArr[row];
//    }else {
//        return self.areaArr[row].regionName;
//    }
    if (component == 0) {
        return self.cityArr[row];
    }else{
        return self.areaArr[row].regionName;
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 30;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
//    if (component == 0) {
//        self.currentSelectProvince = self.provinceArr[row];
//        self.currentSelectCity = nil;
//        self.currentSelectArea = nil;
//        self.selectRegionId = nil;
//        [self calculationCityAreaArr];
//        [pickerView selectRow:[self.cityArr indexOfObject:self.currentSelectCity] inComponent:1 animated:YES];
//        [pickerView selectRow:[self.areaArr indexOfObject:self.currentSelectArea] inComponent:2 animated:YES];
//    }else if (component == 1) {
//        self.currentSelectCity = self.cityArr[row];
//        self.currentSelectArea = nil;
//        [self calculationCityAreaArr];
//        if (self.areaArr.count > 0) {
//            [pickerView selectRow:[self.areaArr indexOfObject:self.currentSelectArea] inComponent:2 animated:YES];
//        }
//
//    }else {
//        self.currentSelectArea = self.areaArr[row];
//
//    }
    if (component == 0) {
        self.currentSelectCity = self.cityArr[row];
        self.currentSelectArea = nil;
        [self calculationCityAreaArr];
        if (self.areaArr.count > 0) {
            [pickerView selectRow:[self.areaArr indexOfObject:self.currentSelectArea] inComponent:1 animated:YES];
        }

    }else {
        self.currentSelectArea = self.areaArr[row];

    }
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    if (!view) {
        view = [[UIView alloc] init];
    }
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, (kScreenWidth - 50) / 2, 30)];
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont systemFontOfSize:20];
    lblTitle.textColor = ColorRGB(51, 51, 51);
//    if (component == 0) {
//        lblTitle.text = self.provinceArr[row];
//    }else if (component == 1) {
//        lblTitle.text = self.cityArr[row];
//    }else {
//        lblTitle.text = self.areaArr[row].regionName;
//    }
    if (component == 0) {
        lblTitle.text = self.cityArr[row];
    }else {
        lblTitle.text = self.areaArr[row].regionName;
    }
    [view addSubview:lblTitle];
    return view;
}
/// 取消
- (void)cancelAction:(UIButton *)btn {
    if (self.cancelBtnAction) {
        self.cancelBtnAction();
    }
    [self removeFromSuperview];
}

/// 确定
- (void)sureAction:(UIButton *)btn {
    
    if (self.confirmSelect) {
        if (self.currentSelectArea) {
           self.confirmSelect(@[self.currentSelectProvince, self.currentSelectCity, self.currentSelectArea]);
        }else{
            LKPickerAreaModel *model = [[LKPickerAreaModel alloc] init];
            self.confirmSelect(@[self.currentSelectProvince, self.currentSelectCity, model]);
        }
    
    }
    [self removeFromSuperview];
}


/**
 计算当前市区数组
 */
- (void)calculationCityAreaArr{
//    [self.provinceArr removeAllObjects];
    [self.cityArr removeAllObjects];
    [self.areaArr removeAllObjects];
    
    if (!self.currentSelectProvince) {
//        [self.provinceArr addObject:@"上海市"];
        self.currentSelectProvince = @"上海市";
    }
    for (LKPickerCityModel *cityModel in self.allDataArr) {
        [self.cityArr addObject:cityModel.regionName];
        if (!self.currentSelectCity) {
            self.currentSelectCity = self.allDataArr[0].regionName;
        }
        if ([self.currentSelectCity isEqual:cityModel.regionName]) {
            for (LKPickerAreaModel *areaModel in cityModel.reginUnitList) {
                if (!self.currentSelectArea) {
                    self.selectRegionId = cityModel.reginUnitList[0].Id;
                    self.currentSelectArea = cityModel.reginUnitList[0];
                }
                [self.areaArr addObject:areaModel];
            }
        }

    }
    [self.pickerV reloadAllComponents];
}
#pragma mark -- Functions
/// 设置view
- (void)setupView {
    // 背景
    self.bgV = [UIButton buttonWithType:(UIButtonTypeSystem)];
    _bgV.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
    _bgV.backgroundColor = ColorRGBA(0, 0, 0, 0.4);
    [_bgV setTitle:@"" forState:(UIControlStateNormal)];
    [_bgV addTarget:self action:@selector(cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self addSubview:_bgV];
    // 承载view
//    UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(20, (kScreenHeight - kPickerViewHeight - 80) / 2, kScreenWidth - 40, kPickerViewHeight + 80)];
    UIView *vv = [[UIView alloc] initWithFrame:CGRectMake(0, (kScreenHeight - kPickerViewHeight - 80), kScreenWidth, kPickerViewHeight + 80)];
    vv.layer.cornerRadius = 10;
    vv.layer.masksToBounds = YES;
    vv.backgroundColor = [UIColor whiteColor];
    [self addSubview:vv];
    // title
    UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, vv.width, kTitleHeight)];
    lblTitle.textColor = ColorRGB(51, 51, 51);
    lblTitle.backgroundColor = [UIColor clearColor];
    lblTitle.text = @"小区选择";
    lblTitle.textAlignment = NSTextAlignmentCenter;
    lblTitle.font = [UIFont systemFontOfSize:17];
    [vv addSubview:lblTitle];
    // 分割线(横)
    UILabel *lblLineH = [[UILabel alloc] initWithFrame:CGRectMake(0, lblTitle.bottom, vv.width, 1)];
    lblLineH.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [vv addSubview:lblLineH];
    // 取消
    UIButton *btnCancel = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btnCancel.frame = CGRectMake(0, lblTitle.top, 100, kTitleHeight);
    btnCancel.backgroundColor = [UIColor clearColor];
    [btnCancel setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btnCancel setTitle:@"取消" forState:(UIControlStateNormal)];
//    [btnCancel setTitleColor:ColorRGB(102, 102, 102) forState:(UIControlStateNormal)];
    [btnCancel addTarget:self action:@selector(cancelAction:) forControlEvents:(UIControlEventTouchUpInside)];
    btnCancel.titleLabel.font = [UIFont systemFontOfSize:17];
    [vv addSubview:btnCancel];
    // 分割线(竖)
    UILabel *lblLineV = [[UILabel alloc] initWithFrame:CGRectMake(btnCancel.right, lblLineH.bottom, 1, btnCancel.height)];
    lblLineV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    lblLineV.backgroundColor = [UIColor clearColor];
    [vv addSubview:lblLineV];
    // 确定
    UIButton *btnSure = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btnSure.frame = CGRectMake(WIDTH - 100 , lblTitle.top, 100, kTitleHeight);
    btnSure.backgroundColor = [UIColor clearColor];
    [btnSure setTitle:@"确定" forState:(UIControlStateNormal)];
    [btnSure setTitleColor:ColorRGB(83, 184, 255) forState:(UIControlStateNormal)];
    [btnSure addTarget:self action:@selector(sureAction:) forControlEvents:(UIControlEventTouchUpInside)];
    btnSure.titleLabel.font = [UIFont systemFontOfSize:17];
    [vv addSubview:btnSure];
    self.pickerV = [[UIPickerView alloc] initWithFrame:CGRectMake(0, lblTitle.bottom, vv.width, kPickerViewHeight)];
    self.pickerV.frame = CGRectMake(0, btnCancel.bottom, vv.width, kPickerViewHeight);
    self.pickerV.delegate = self;
    self.pickerV.dataSource = self;
    self.pickerV.backgroundColor = [UIColor clearColor];
    [vv addSubview:_pickerV];
}

/**
 获取三级联动的数据表
 */
-(void)getDataList{
    LKUserInfor *user = [LKUserInforTool account];
<<<<<<< HEAD:LKIntelligentVideoProject/LKIntelligentVideoProject/Lib/AddressPicker/LKAddressPickerView.m
    NSDictionary *param = @{@"roleId":user.roleId};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerUrl apiPath:@"/openHikApi/server/app/selectByPrentIdList/permissions" parameters:param progress:^(NSProgress * _Nullable progress) {
=======
    NSDictionary *param = @{@"loginAccount":@"admin"};
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerUrl apiPath:@"/WechatVisitors/openHikApi/selectByPrentIdList/permissions" parameters:param progress:^(NSProgress * _Nullable progress) {
>>>>>>> parent of 2e14138... 提交:LKIntelligentVideoProject/LKIntelligentVideoProject/Lib/LKAddressPickerView.m

    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        NSArray *dataArra = responseObject[@"data"];
        [self.provinceArr addObject:@"上海市"];
        for (NSDictionary *dic in dataArra) {
            LKPickerCityModel *model = [LKPickerCityModel modelWithDictionary:dic];
            NSMutableArray *reginUnitArray = [model.reginUnitList mutableCopy];
            LKPickerAreaModel *arModel = [[LKPickerAreaModel alloc] init];
            arModel.Id = model.Id;
            arModel.regionName = @"全部";
            [self.areaArr addObject:arModel];
            [reginUnitArray insertObject:arModel atIndex:0];
            model.reginUnitList = [reginUnitArray copy];
            [self.allDataArr addObject:model];
//            [self.cityArr addObject:model.regionName];
//            LKPickerAreaModel *arModel = [[LKPickerAreaModel alloc] init];
//            arModel.Id = model.Id;
//            arModel.regionName = @"全部";
//            [self.areaArr addObject:arModel];
//            for (LKPickerAreaModel *areaModel in model.reginUnitList) {
//                [self.areaArr addObject:areaModel];
//            }
        }
        [self calculationCityAreaArr];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
    

}
#pragma mark -- Getter
-(NSMutableArray *)allDataArr{
    if (!_allDataArr) {
        _allDataArr = [[NSMutableArray alloc] init];
    }
    return _allDataArr;
}

- (NSMutableArray *)provinceArr {
    if (!_provinceArr) {
        _provinceArr = [NSMutableArray array];
    }
    return _provinceArr;
}

- (NSMutableArray *)cityArr {
    if (!_cityArr) {
        _cityArr = [NSMutableArray array];
    }
    return _cityArr;
}

- (NSMutableArray *)areaArr {
    if (!_areaArr) {
        _areaArr = [NSMutableArray array];
    }
    return _areaArr;
}
@end
